<?php
namespace app\store\controller\parent;


use app\store\controller\Base;
use app\store\controller\Weixin;
use think\Db;
use think\Session;
header("Content-type:text/html;charset=utf-8");

class Index extends Base
{
	/**
	 * 家长端首页
	 * URL : store/parent.index/index
	 * @return [type] [description]
     * wangtiantian
	 */

    public function index()
    {

        //广告图
        $banner_info =  Db('banner')
            ->where('shop_id',$this->shop_id)
            ->where('is_show',1)
            ->where('is_del',0)
            ->field('pic_url,url')
            ->select();
        $this->assign('banner_info',$banner_info);

        //公告
        $notice_info = Db('notice')
            ->where('shop_id',$this->shop_id)
            ->where('is_show',1)
            ->field('title')
            ->select();
        $this->assign('notice_info',$notice_info);

        //微信 js config
        $weixin = new Weixin($this->shop_id);
        $url = request()->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);
        $this->assign('jsconfig', $js_config);
        $this->assign('shop_id',$this->shop_id);

       return $this->fetch();
    }

    /**
     * 个人中心
     * wangtiantian
     */
    public function mycenter(){
        $res = Db::name('parent')->where('id',$this->uid)->find();
        $this->assign('res',$res);
        //微信 js config
        $weixin = new Weixin($this->shop_id);
        $url = request()->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);
        $this->assign('jsconfig', $js_config);
        $this->assign('shop_id',$this->shop_id);
        return $this->fetch('myCenter');
    }

    /*
     * 信息编辑页
     * wangtiantian
     */

    public function edit(){

        if(request()->isAjax()){
            $d = input();
            $data['headimgurl'] = $d['headimgurl'];
            $data['tel'] = $d['tel'];
            $data['wx_name'] = $d['wx_name'];
            $data['realname'] = $d['realname'];
            $data['sex'] = $d['sex'];



            if($data['headimgurl'] == null || $data['headimgurl'] == "undefined"){

                $data['headimgurl'] = Db::name('parent')->where('id',$this->uid)->value('headimgurl');
            }

            $res = Db::name('parent')->where('id',$this->uid)->update($data);

            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }


        }

        $res= Db::name('parent')->where('id',$this->uid)->find();
        //微信 js config
        $weixin = new Weixin($this->shop_id);
        $url = request()->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);
        $this->assign('jsconfig', $js_config);
        $this->assign('res', $res);
        $this->assign('shop_id',$this->shop_id);
        return  $this->fetch();
    }


    /**
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * 绑定学生二维码
     */
    public function student_bind(){

        if (request()->isAjax()){
            $data = json_decode(input('data'),true);

            if($data['errMsg'] == "scanQRCode:ok"){
               $stu_no = $data['resultStr'];
               // 获取学生信息
               $stu_info = Db::name('student')->where('stu_no',$stu_no)->find();
               $par =Db::name('parent')->where('id',$this->uid)->find();
               $bind = Db::name('parent_bind_student')
                        ->where('stu_id',$stu_info['stu_id'])
                        ->where('parent_openid',$par['openid'])
                        ->find();

               if($stu_info){
//                   $pre['openid'] = $this->openid;
//                   $pre['jx_openid'] = $this->jx_openid;

                   if($bind){
                       $this->error('该学生已绑定，请勿重复操作');
                   }else{
                       if ($par['openid'] == null  && $par['jx_openid'] == null){

//                           $p =Db::name('parent')->where('id',$this->uid)->update($pre);
                           $parent =Db::name('parent')->where('id',$this->uid)->find();
//                           if($p){
//                               $flag = 1;
//                           }else{
//                               $flag = 0;
//                           }
                           $b['parent_openid'] = $parent['openid'];
                           $b['stu_id']        = $stu_info['stu_id'];
                           $b['create_time']   = time();

                           $p_bind_s = Db::name('parent_bind_student')->insertGetId($b);

                           if($p_bind_s){
                               $flag = 1;
                           }else{
                               $flag = 0;
                           }

                           if($flag){
                               $this->success("绑定成功");
                           }else{
                               $this->error('绑定失败');
                           }
                       }
                       else{
                           $parent =Db::name('parent')->where('id',$this->uid)->find();
                           $b['parent_openid'] = $parent['openid'];
                           $b['stu_id']        = $stu_info['stu_id'];
                           $b['create_time']   = time();
                           $p_bind_s = Db::name('parent_bind_student')->insertGetId($b);
                           if($p_bind_s){
                               $this->success("绑定成功");
                           }else{
                               $this->error('绑定失败');
                           }
                       }
                   }

               }else{
                   $this->error('无此学生信息，无法绑定');
               }

            }

        }

    }


    public function parent_info(){
        if(input()){
            $wx_info = json_decode(input('back_info'),true);

            $data['openid'] =  $wx_info['openid'];
            $data['wx_name'] =  $wx_info['nickname'];
            $data['headimgurl'] =  $wx_info['headimgurl'];
            $data['sex'] =  $wx_info['sex'];
            $data['create_time'] =  time();
            $data['shop_id'] =  input('shop_id');

            $test = Db::name('parent')->where('openid',$data['openid'])->find();

            if($test==null){

                $res = Db::name('parent')->insertGetId($data);
                if($res){
                    Session::set('shop_id',$data['shop_id'] );
                    Session::set('uid',$res );
                    $this->success('绑定成功','index');
                }else{
                    $this->error('绑定失败');
                }

            }
else{
//                Session::set('shop_id',$test['shop_id'] );
//                Session::set('uid',$test['teacher_id'] );
                $this->error('您已绑定，请勿重复操作');
            }



        }

    }




}
